package com.serkol.ams.controller.api;

import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.AdvertisingManagement;
import com.serkol.ams.service.AdvertisingManagementService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/advertisingManagement")
public class AdvertisingManagementApiController {

    @Autowired
    private AdvertisingManagementService advertisingManagementService;
    @Value("${upload.host}")
    private String uploadhost;
    @IsLogin
    @GetMapping("/findByBackList")
    @ResponseBody
    public Result findByBackList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current, @RequestParam(name = "state", defaultValue = "0") int state) {
        AuthConstant.setPathUrl(uploadhost);
        return advertisingManagementService.findByBackList(pageSize,current, state);
    }


    @GetMapping("/findByMiniList")
    @ResponseBody
    public Result findByMiniList(@RequestParam(name = "type", defaultValue = "0") int type) {
        AuthConstant.setPathUrl(uploadhost);
        List<Map<String, Object>> info = advertisingManagementService.findByMiniList(type);
        return Result.ok(info);
    }

    @GetMapping("/findByMiniByType")
    @ResponseBody
    public Result findByMiniByType(@RequestParam(name = "type", defaultValue = "0") int type) {
        AuthConstant.setPathUrl(uploadhost);
      Map<String, Object>   info=advertisingManagementService.findByMiniByType(type);
        return Result.ok(info);
    }

    @IsLogin
    @PostMapping("/edit")
    public Result edit(@RequestBody AdvertisingManagement data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = advertisingManagementService.updateById(data);
        } else {
            flag = advertisingManagementService.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }
    @IsLogin
    @PostMapping("/byDelete")
    @ResponseBody
    public Result byDelete(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {
        if(id>0) {
            boolean b = advertisingManagementService.byDelete(id);
            if (b) {
                return Result.ok();
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
            }
        }else{
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_FOUND);
        }
    }
}
